﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.ComponentModel
Imports System.Windows
Imports VMWorkshop.Domain
Imports AutoMapper

Public Class DemoViewModel
    Implements INotifyPropertyChanged
    ' Events
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    ' Private fields
    Private _products As IList(Of ProductListView)
    Private _selectedProduct As ProductEditView
    Private _repository As IProductRepository
    Private _editFormVisibility As Visibility = Visibility.Collapsed
    Private _updateMessageVisibility As Visibility = Visibility.Collapsed

    ' Public properties
    Public Property EditFormVisibility() As Visibility
        Get
            Return Me._editFormVisibility
        End Get
        Set(ByVal value As Visibility)
            If Me._editFormVisibility <> value Then
                Me._editFormVisibility = value
                Me.RaisePropertyChangedEvent("EditFormVisibility")
            End If
        End Set
    End Property

    Public Property UpdateMessageVisibility() As Visibility
        Get
            Return Me._updateMessageVisibility
        End Get
        Set(ByVal value As Visibility)
            If Me._updateMessageVisibility <> value Then
                Me._updateMessageVisibility = value
                Me.RaisePropertyChangedEvent("UpdateMessageVisibility")
            End If
        End Set
    End Property

    Public Property Products() As IList(Of ProductListView)
        Get
            If Me._products Is Nothing Then
                Me._products = Me.GetProducts()
            End If
            Return Me._products
        End Get
        Set(ByVal value As IList(Of ProductListView))
            If Me._products IsNot value Then
                Me._products = value
                Me.RaisePropertyChangedEvent("Products")
            End If
        End Set
    End Property

    Public Property SelectedProduct() As ProductEditView
        Get
            Return Me._selectedProduct
        End Get
        Set(ByVal value As ProductEditView)
            If Me._selectedProduct IsNot value Then
                Me._selectedProduct = value
                Me.RaisePropertyChangedEvent("SelectedProduct")
            End If
        End Set
    End Property

    ' Constructor
    Public Sub New(ByVal repository As IProductRepository)
        Me._repository = repository
    End Sub

    ' Public API methods
    Public Sub Persist()
        Dim newProduct As Product = ProductEditView.ToProduct(Me.SelectedProduct)
        Dim oldProduct As Product = Me._repository.GetProductById(newProduct.ProductId)

        With oldProduct
            .Title = newProduct.Title
            .Description = newProduct.Description
            .QuantityOnHand = newProduct.QuantityOnHand
            .ReleaseDate = newProduct.ReleaseDate
            .Price = newProduct.Price
        End With

        Me._repository.Persist(oldProduct)

        Me.UpdateMessageVisibility = Visibility.Visible
        Me.EditFormVisibility = Visibility.Collapsed
    End Sub

    Public Sub GetSelectedProduct(ByVal productId As Integer)
        Dim product As Product = Me._repository.GetProductById(productId)
        Me.SelectedProduct = ProductEditView.ToProductEditView(product)

        Me.EditFormVisibility = Visibility.Visible
        Me.UpdateMessageVisibility = Visibility.Collapsed
    End Sub

    ' Utilities
    Private Function GetProducts() As IList(Of ProductListView)
        Dim products As IList(Of Product) = Me._repository.GetAll()
        Return Mapper.Map(Of IList(Of Product), IList(Of ProductListView))(products)
    End Function

    Protected Overridable Sub RaisePropertyChangedEvent(ByVal ParamArray propertyNames As String())
        For Each name As String In propertyNames
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
        Next
    End Sub
End Class